﻿using GalaSoft.MvvmLight.Messaging;
using JiveMessenger.Models;
using JiveMessenger.User;
using JiveMessenger.ViewModels;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace JiveMessenger.AvalonEdit
{
    class AnimatedEmoticonImage : Image
    {
        private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
        private string _imageFileName;
        private string _key;
        private IClient _currentClient;
        private ViewModelChat _chatViewModel;
        public AnimatedEmoticonImage(BitmapSource source,ViewModelChat chat, IClient currentClient, string imageFilename, string emoticonKey)
            : base()
        {
            _currentClient = currentClient;
            _chatViewModel = chat;
            this.Source = source;
            this.Width = source.PixelWidth;
            this.Height = source.PixelHeight;

            _key = emoticonKey;
            _imageFileName = imageFilename;

            SetAnimatedSource(source);
        }
        protected override void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
            Image tw = e.OriginalSource as Image;

            if (e.Handled
                || e.RightButton != MouseButtonState.Pressed
                || this._key == "<spacer>"
                || this._key == "<line>"
                || this._key == "<s_music>"
                || this._key == "</s_music>"
                || this._key == "<w_music>"
                || this._key == "</w_music>"
                || tw == null)
            {
                base.OnMouseDown(e);
                return;
            }
            try
            {
                ContextMenu menu = new ContextMenu();
                menu.Items.Clear();
                MenuItem mi = new MenuItem { Header = "Add" };
                mi.Click += this.mi_Click;
                menu.Items.Add(mi);
                if (this._currentClient.GetEmoticons(_currentClient.CurrentUserInfo.UserID, true, true, false).Values.Any(emoticon => emoticon.EmoticonPath == this._imageFileName))
                {
                    mi.IsEnabled = false;
                }
                menu.IsOpen = true;
                e.Handled = true;
            }
            catch (Exception ex)
            {
                Logger.ErrorException("Exception in image mousedown", ex);
            }
        }

        private void mi_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MenuItem mi = (MenuItem)sender;
            Messenger.Default.Send(new NotificationMessage<IEmoticon>(_chatViewModel,new JiveCustomEmoticonModel {  EmoticonPath = _imageFileName, EmoticonShortcut = _key }, "AddEmoticon"));
        }

        public void SetAnimatedSource(ImageSource source)
        {
            WpfAnimatedGif.ImageBehavior.SetAnimatedSource(this, source);
            WpfAnimatedGif.ImageBehavior.SetAutoStart(this, true);
            //WpfAnimatedGif.ImageBehavior.SetAutoStart(this, false);
        }
    }
}
